package com.mofang.powerdekorhelper.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by luofei on 2017/7/26 0026.
 */

public class NetUtiles {

    private File file;

    public Bitmap getBitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);

            /*BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;*/

            bm = BitmapFactory.decodeStream(bis, null, null);
            bis.close();
            is.close();// 关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }


    public File bitmapCorvert(Context context, String imageUrl, int mainTitle, final String mainTitle2, String subTitle, final ICorvertImage iCorvertImage) {

        final View qrcodeView = LayoutInflater.from(context).inflate(R.layout.qrcode_with_text_layout, null);

        if (mainTitle != -1) {
            ((TextView) qrcodeView.findViewById(R.id.qrcode_title)).setText(mainTitle);
        } else {
            ((TextView) qrcodeView.findViewById(R.id.qrcode_title)).setText(mainTitle2);
        }

        if (subTitle == null) {
            (qrcodeView.findViewById(R.id.qrcode_descripton)).setVisibility(View.GONE);
        } else {
            ((TextView) qrcodeView.findViewById(R.id.qrcode_descripton)).setText(subTitle);
        }

        final ImageView newQrcodeImv = (ImageView) qrcodeView.findViewById(R.id.qrcode_img);

        new AsyncTask<String, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(String... params) {

                Bitmap bitmap = new NetUtiles().getBitmap(params[0]);

                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {

                super.onPostExecute(bitmap);

                if (bitmap != null) {

                   /* newQrcodeImv.setImageBitmap(bitmap);

                    L.i("GetWidth", "newQrcodeImv.getWidth()------------>" + newQrcodeImv.getWidth());

                    Bitmap newBitmap = ImageUtils.getViewBitmap(qrcodeView, bitmap.getWidth(), 400);*/

                    Bitmap newBitmap = new CreatePicWithText().createPic(bitmap, mainTitle2);

                    file = saveBitmap(newBitmap);

                    iCorvertImage.corvertSuccess(file);
                } else {
                    iCorvertImage.covertFail();
                }
            }
        }.execute(imageUrl);

        return null;
    }

    public List<Bitmap> getImageBitmap(Context context, String imageUrl, String bgImageUrl, final ICorvertImage2 iCorvertImage) {

        final List<Bitmap> bitmapList = new ArrayList<>();

        new AsyncTask<String, Void, List<Bitmap>>() {
            @Override
            protected List<Bitmap> doInBackground(String... params) {

                Bitmap bitmap1 = new NetUtiles().getBitmap(params[0]);
                Bitmap bitmap2 = new NetUtiles().getBitmap(params[1]);

                bitmapList.add(bitmap1);
                bitmapList.add(bitmap2);

                return bitmapList;
            }

            @Override
            protected void onPostExecute(List<Bitmap> bitmap) {

                super.onPostExecute(bitmap);

                if (bitmap != null) {

                    if (bitmap.size() > 1) {
                        iCorvertImage.corvertSuccess(bitmap);
                    }
                } else {
                    iCorvertImage.covertFail();
                }
            }
        }.execute(imageUrl, bgImageUrl);

        return null;
    }

    public File mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
        Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(), firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);
        canvas.drawBitmap(ImageUtils.small(secondBitmap), 230, 670, null);
        File file = saveBitmap(bitmap);
        return file;
    }


    /**
     * 保存方法
     */
    public File saveBitmap(Bitmap bitmap) {

        File f = new File(SDCardUtils.getPath("saveImage"), "shared_qrcode.png");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return f;
    }

    public interface ICorvertImage {
        void corvertSuccess(File file);

        void covertFail();
    }

    public interface ICorvertImage2 {
        void corvertSuccess(List<Bitmap> bitmap);

        void covertFail();
    }
}
